﻿using QQExpression.Utils.Currency;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QQExpression.Utils.Type
{
    class ByteHelper
    {

        /// <summary>
        /// 将 Stream 转成 byte[]  bitmap
        /// </summary>
        /// <param name="TempStream"></param>
        /// <returns></returns>
        public byte[] BitmapStreamToBytes(Stream TempStream)
        {
            byte[] imgByte = new byte[TempStream.Length];
            TempStream.Position = 0;
            TempStream.Read(imgByte, 0, Convert.ToInt32(TempStream.Length));//反序列，存放在字节数组里



            return imgByte;
        }
        /// <summary>
        /// String转byte[]  采用格式：UTF8
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public byte[] StrTobyte(string Text)
        {
            return System.Text.Encoding.UTF8.GetBytes(Text);
        }

        /// <summary>
        /// Byte转string  采用格式：UTF8
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>

        public string ByteToStr(byte[] byteArray)
        {
            return System.Text.Encoding.UTF8.GetString(byteArray);
        }

     

        /// <summary>
        /// 将文件转换为byte数组
        /// </summary>
        /// <param name="path">文件地址</param>
        /// <returns>转换后的byte数组</returns>
        public byte[] FileToBytes(string path)
        {
            if (!System.IO.File.Exists(path))
            {
                return new byte[0];
            }

            FileStream stream = new FileInfo(path).OpenRead();
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return buffer;
        }
        /// <summary>
        /// 拼接Byte
        /// </summary>
        /// <param name="FistSection"></param>
        /// <param name="EndSection"></param>
        static void ByteMerge(byte[] FistSection,byte[] EndSection)
        {
            List<byte> FistSectionList = FistSection.ToList();

            Stopwatch sw = new Stopwatch();
            sw.Start();
            FistSectionList.AddRange(EndSection.ToList());
            byte[] data = FistSectionList.ToArray();
           
            sw.Stop();
            Console.WriteLine("ListTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度：" + data.Length);
        }
        /// <summary>
        /// 转换byte，存储为文件
        /// </summary>
        /// <param name="ByteArray"></param>
        /// <param name="SavePath"></param>
        /// <returns></returns>
        public bool ByteToFile(byte[] ByteArray, string SavePath)
        {
            try
            {
                using (FileStream fs = new FileStream(SavePath, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(ByteArray, 0, ByteArray.Length);

                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);

                return false;
            }
            return true;
        }

    }
}
